<include file='Index/header' />
<div class="row">
	<br>
	<div class="col-sm-12" >
    	<button type="button" data-toggle="modal" class="btn btn-primary" data-target="#district-detail" data-backdrop="static">新增小区</button>
    	<br><br>
   </div>

	<div class="col-sm-12">
		<div class="col-sm-2">
			<ul id="repositoryTree" class="ztree"></ul>
		</div>
		<div class="col-sm-10">
		<table id="districtTable" class="table table-hover table-bordered table-striped table-striped">
			<thead>
				<tr>
					<td>A仓库</td>
					<td>B仓库</td>
					<td>编号</td>
					<td>小区名称</td>
					<td>小区地址</td>
					<td>所属区</td>
					<td>排序</td>
					<td style="width:21%">操作</td>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
		</div>
	</div>
	<br>
</div>

<include file='Index/footer' />
<include file="District/district-detail" />
<script>
$(document).ready(function() {
		loadRepositoryTree('repositoryTree', true, true);
		loadRepositoryTree('repositoryTree1', false);
		loadTable();
});
var table;
function loadTable(){
	table = $('#districtTable').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
		"ajax":'{:U("Admin/District/getDistrict/")}',
		"columns": [
					{ "data": 'a_repository_id' },
					{ "data": 'b_repository_id' },
	        { "data": 'id' },
	        { "data": 'name' },
	        { "data": 'address' },
	        { "data": 'region' },
	        { "data": 'index' },
	        { "data": 'btn' }
    ],
    "columnDefs": [
    {
    	"targets": 0,
    	"visible":false
    },
    {
    	"targets": 1,
    	"visible":false
    },
    {
    	"targets": 2,
    	"render": function (data, type, row, table) {
        return table.row + 1;
      }
    },
    {
    	"targets": -1,
    	"data": null,
    	"render": function (data, type, row, table) {
    		var btn = '暂停下单';
    		var status = '1';
    		if(row.status != '0'){
					btn = '开始下单';
					status = '0';
    		}
        return '<button class="btn btn-info" onclick="edit(\''+row.district_id+'\')">编辑</button>  <button class="btn btn-danger" onclick="del(\''+row.district_id+'\')">删除</button>';
      },
    }]
	});
}

function edit(district_id){
	//编辑按钮点击，弹出编辑框
	$.ajax({
			url: '{:U("Admin/District/getDistrict/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"district_id":district_id
			}
		})
		.success(function(data){
			var treeObj = $.fn.zTree.getZTreeObj("repositoryTree1");
			var parentNode = treeObj.getNodeByParam("id", data.b_repository_id);
			var node = treeObj.getNodeByParam("id", data.b_repository_id+data.a_repository_id, parentNode);

			treeObj.selectNode(node);
			$("input[name='district_id']").attr('value', data.district_id);
			$("input[name='name']").attr('value', data.name);
			$("input[name='region']").attr('value', data.region);
			$("input[name='address']").attr('value', data.address);
			$("input[name='index']").attr('value', data.index);

			$('#district-detail').modal({
				backdrop : "static"
			});
	});
}

function del(district_id){
	if(!confirm("确认删除该小区吗？")){
		return;
	}
	$.ajax({
			url: '{:U("Admin/District/deleteDistrict/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"district_id":district_id
			}
		})
		.success(function(data){
			location.reload();
	});
}

function searchARepository(val){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(0).search(val).draw();
}

function searchBRepository(val){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(1).search(val).draw();
}

</script>
